package com.ict.web.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.api.client.util.Lists;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.ProfessionalQualification;
import com.ict.web.dto.InsertExpertInformationSheetDto;
import com.ict.web.dto.InsertProfessionalQualificationDto;
import com.ict.web.dto.UpdateProfessionalQualificationDto;
import com.ict.web.mapper.ProfessionalQualificationMapper;
import com.ict.web.service.IProfessionalQualificationService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 职业资格Service业务层处理
 * 
 * @author liujie
 * @date 2022-02-23
 */
@Service
public class ProfessionalQualificationServiceImpl extends ServiceImpl<ProfessionalQualificationMapper,ProfessionalQualification> implements IProfessionalQualificationService
{
    @Resource
    private ProfessionalQualificationMapper professionalQualificationMapper;

    @Resource
    private IYzqFileService fileService;

    /**
     * 查询职业资格
     * 
     * @param professionalId 职业资格主键
     * @return 职业资格
     */
    @Override
    public ProfessionalQualification selectProfessionalQualificationByProfessionalId(Long professionalId)
    {
        return professionalQualificationMapper.selectProfessionalQualificationByProfessionalId(professionalId);
    }

    /**
     * 查询职业资格列表
     * 
     * @param
     * @return 职业资格
     */
    @Override
    public List<ProfessionalQualification> selectProfessionalQualificationList()
    {
        return professionalQualificationMapper.selectProfessionalQualificationList();
    }

    /**
     * 新增职业资格
     * 
     * @param addDtoS 职业资格
     * @return 结果
     */
    @Override
    public void  insertProfessionalQualification(Long expertId, List<InsertProfessionalQualificationDto> addDtoS)
    {
        if (!CollectionUtils.isEmpty(addDtoS)){
            List<ProfessionalQualification> beans = Lists.newArrayList();
            for (InsertProfessionalQualificationDto qualifications :addDtoS){
                ProfessionalQualification bean = InsertProfessionalQualificationDto.of(qualifications);
                //同步文件对象
                ArrayList<YzqFile> fileArrayList = InsertProfessionalQualificationDto.toFile(bean, qualifications);
                fileService.batchInsertFile(fileArrayList);
                bean.setExpertId(expertId);
                beans.add(bean);
//                beans.add(new ProfessionalQualification(IdUtils.snowId(),expertId,qualifications.getQualificationName(),
//                        qualifications.getQcNo(),qualifications.getRegisteredUnit(),qualifications.getRegistrationTime(),
//                        qualifications.getExpirationTime(),qualifications.getQualificationFile(),ProfessionalQualification.DELETE_STATUS_NORMAL,
//                        LocalDateTime.now(),LocalDateTime.now()));
            }
            this.saveBatch(beans);
        }
    }

    /**
     * 修改职业资格
     * 
     * @param dto 职业资格
     * @return 结果
     */
    @Override
    public int updateProfessionalQualification(UpdateProfessionalQualificationDto dto)
    {
      ProfessionalQualification bean = UpdateProfessionalQualificationDto.of(dto);
        return professionalQualificationMapper.updateById(bean);
    }


    /**
     * 删除职业资格信息
     * 
     * @param professionalId 职业资格主键
     * @return 结果
     */
    @Override
    public int deleteProfessionalQualificationByProfessionalId(Long professionalId)
    {
        ProfessionalQualification bean =professionalQualificationMapper.selectById(professionalId);
        bean.setDelFlag("1");
        return professionalQualificationMapper.updateById(bean);
    }
}
